#include "backwidget.h"
#include "ui_backwidget.h"
#include "addmqttdialog.h"
#include "analogwidget.h"
#include "switchwidget.h"
#include "textwidget.h"

BackWidget::BackWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BackWidget)
{
    ui->setupUi(this);
    this->setFocusPolicy(Qt::StrongFocus);       //鼠标点击聚焦

    QPalette palettle(QColor(0,0,0,50));
    this->setAutoFillBackground(true);
    this->setPalette(palettle);
}

BackWidget::~BackWidget()
{
    delete ui;
}

void BackWidget::setClient(QMQTT::Client *client)
{
    if(!client) return;
    mqttClient = client;
}

void BackWidget::focusInEvent(QFocusEvent *e)
{
    Q_UNUSED(e)
    //this->setStyleSheet("background-color: rgb(220,220, 220)");
}

void BackWidget::focusOutEvent(QFocusEvent *e)
{
    Q_UNUSED(e)
    //this->setStyleSheet("background-color: rgb(240,240, 240)");
}
#include "qevent.h"
void BackWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::RightButton)
        showMenu(mapToGlobal(event->pos()));
}
#include <QMenu>
void BackWidget::showMenu(const QPoint &pos)
{
    QMenu menu;
    menu.addAction("设置背景",[=](){

    });
    menu.addAction("删除背景",[=](){

    });
    menu.addAction("新增背景",[=](){

    });

    menu.addAction("添加控件",[=](){
        AddMqttDialog addDialog("新增",this);
        if(addDialog.exec() == QDialog::Rejected)
            return ;
        addMqttWidget(mapFromGlobal(pos),addDialog.getData());
    });

    menu.addAction("清空控件",[=](){
        QMessageBox message(QMessageBox::Question,"询问","确定清空所有控件？",QMessageBox::Yes|QMessageBox::No);
        if(message.exec() == QMessageBox::Yes)
            removMqttWidgets(mqttWidgetMap.keys());
    });
    menu.exec(pos);
}

void BackWidget::addMqttWidget(const QPoint &pos,MqttData data)
{
    static int id = -1;
    int count = mqttWidgetMap.keys().count();
    if(id < 0 || id < count)
        id = count;
    id ++;

    //确定类型、信号质量、topic、是否可public
    MqttWidget *buff = nullptr;
    if(data.type == MqttData::eSwitch)
        buff = new SwitchWidget(this);  //开关量对象
    else if(data.type == MqttData::eAnalog)
        buff = new AnalogWidget(this);  //模拟量对象
    else if(data.type == MqttData::eText)
        buff = new TextWidget(this);    //文本对象
    else
        buff = new TextWidget(this);   ///默认为文本
    if(!buff)
        return;

    buff->move(pos);
    buff->id = id;
    buff->setData(data);
    mqttWidgetMap.insert(id,buff);
    if(mqttClient)
        mqttClient->unsubscribe(mqttWidgetMap[id]->mData.topic);
    //删除操作
    connect(buff,&MqttWidget::deleteThis,this,[=](int id)
    {
        if(!removMqttWidget(id))
            QMessageBox::information(this,"提示","删除失败");
    });
}

//删除控件
bool BackWidget::removMqttWidget(int id)
{
    if(mqttWidgetMap.keys().contains(id))
    {
        if(mqttClient)
            mqttClient->unsubscribe(mqttWidgetMap[id]->mData.topic);
        delete mqttWidgetMap[id];
        mqttWidgetMap.remove(id);
        return 1;
    }
    return 0;
}

//清空控件
void BackWidget::removMqttWidgets(QList<int> idList)
{
    for(int id : idList)
        removMqttWidget(id);
}

void BackWidget::receivedMessage(QMQTT::Message message)
{
    for(int id: mqttWidgetMap.keys())
    {
        QString myTopic = mqttWidgetMap[id]->mData.topic;
        if(message.topic() == myTopic)
            mqttWidgetMap[id]->getMessage(message);
    }
}


